/**
 * 
 */
package com.xbang.attendance.web.action.user.usermanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import com.xbang.attendance.entity.role.Role;
import com.xbang.attendance.entity.role.User;
import com.xbang.attendance.service.UserManagerService;
import com.xbang.attendance.utils.MD5;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.service.SendMailService;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class UserManager extends SearchActionSupport {

	private final static Log logger = LogFactory.getLog(UserManager.class);

	@Autowired
	private UserManagerService userManagerService;

	@Autowired
	private SendMailService sendMailService;

	private List<Role> roles = new ArrayList<Role>();

	private List<User> users = new ArrayList<User>();

	private List<Integer> roleIds = new ArrayList<Integer>();

	private User user;

	private Integer id;

	private String sendMailResult;

	@Action("/user/usermanager/mailtest")
	public String sendMail() throws Exception {

//		sendMailService.sendMail("", "", "");

		return "r:/user/usermanager/list";
	}

	@Action("/user/usermanager/save")
	public String save() throws Exception {
		if (null != user.getPassword() && null != user.getRepassword()) {
			if (user.getPassword().equals(user.getRepassword())) {
				if (userManagerService.isDuplicateByUserName(user.getName())) {
					// 重复
				} else {
					// save.
					user.setPassword(MD5.crypt(user.getPassword()));
					userManagerService.save(user);
				}
			}
		}
		return "r:/user/usermanager/list";
	}

	@Action("/user/usermanager/add")
	public String add() throws Exception {
		return "detail";
	}

	@Action("/user/usermanager/update")
	public String update() throws Exception {

		userManagerService.update(user);
		return "r:/user/usermanager/list";
	}

	@Action("/user/usermanager/${id}/edit")
	public String detail() throws Exception {
		if (id != null) {
			user = (User) userManagerService.getObject(User.class, id);
		}
		return "detail";
	}

	@Action("/user/usermanager/${id}/modifyrole")
	public String modifyrole() throws Exception {

		if (id != null) {
			user = (User) userManagerService.getObject(User.class, id);

			roles = userManagerService.getObjects(Role.class);

			for (Role role : roles) {
				role.setRoles(user.getRoles());
			}
		}

		return "modifyRole";
	}

	@Action("/user/usermanager/${id}/delete")
	public String delete() throws Exception {

		if (null != id) {
			userManagerService.delete(User.class, id);
		}

		return "r:/user/usermanager/list";
	}

	@Action("/user/usermanager/search")
	public String saerch() throws Exception {
		//
		return "search";
	}

	@Action("/user/usermanager/search/do")
	public String doSearch() throws Exception {

		String hqlSentence = "FROM User user WHERE 1 = 1";

		Result result = userManagerService.searchByPage(hqlSentence, searchComponent, page);

		users = result.getItems();
		page = result.getPage();

		return "index";
	}

	@Action("/user/usermanager/savemodifyrole")
	public String savemodifyrole() throws Exception {

		// //user.

		if (null != roleIds && !roleIds.isEmpty()) {
			user = (User) userManagerService.getObject(User.class, user.getId());

			String hqlSentence = "FROM Role role WHERE role.id in (:roleId)";

			Set<Integer> roleIdSets = new HashSet<Integer>();
			for (Integer roleId : roleIds) {
				roleIdSets.add(roleId);
			}

			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("roleId", roleIdSets);

			List<Role> tmpRoles = userManagerService.searchForList(hqlSentence, parameters);
			Map<String, Role> userRoles = new HashMap<String, Role>();
			for (Role tmpRole : tmpRoles) {
				userRoles.put(tmpRole.getName(), tmpRole);
			}

			user.setRoles(userRoles);
			userManagerService.save(user);
		}

		return "r:/user/usermanager/list";
	}

	@Action("/user/usermanager/list")
	public String list() throws Exception {

		String hqlSentence = "FROM User user WHERE 1 = 1 ";

		Result result = userManagerService.searchByPage(hqlSentence, searchComponent, page);

		users = result.getItems();
		page = result.getPage();

		return "index";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xbang.core.struts.support.SearchActionSupport#resetCustomerSearchCriteria()
	 */
	@Override
	public void resetCustomerSearchCriteria() {

	}

	/**
	 * @param userManagerService
	 */
	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	/**
	 * @return the users
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * @param users
	 *            the users to set
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}

	/**
	 * @return the roles
	 */
	public List<Role> getRoles() {
		return roles;
	}

	/**
	 * @param roles
	 *            the roles to set
	 */
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the roleIds
	 */
	public List<Integer> getRoleIds() {
		return roleIds;
	}

	/**
	 * @param roleIds
	 *            the roleIds to set
	 */
	public void setRoleIds(List<Integer> roleIds) {
		this.roleIds = roleIds;
	}

}
